<template>
  <div class="hy-form">
    <div class="header">
      <slot name="header"></slot>
    </div>

    <el-form :label-width="labelWidth" ref="formRef" :model="formData">
      <el-row>
        <template v-for="item in formItems" :key="item.label">
          <el-col v-bind="colLayout">
            <!-- 自定义 el-form-item 插槽-->
            <template v-if="item.type === 'itemSlot'">
              <slot :name="item.slotName" v-bind="{ ...item, itemStyle }">
              </slot>
            </template>

            <el-form-item
              v-else
              :prop="item.field"
              :label="item.label"
              :rules="item.rules"
              :style="itemStyle"
            >
              <template
                v-if="
                  item.type === 'input' ||
                  item.type === 'password' ||
                  item.type === 'textarea'
                "
              >
                <el-input
                  :placeholder="item.placeholder"
                  :type="item.type"
                  v-bind="item.otherOptions"
                  :show-password="item.type === 'password'"
                  v-model="formData[`${item.field}`]"
                  @change="
                    item.inputType &&
                      inputChange(item.inputType, item, item.field)
                  "
                />
              </template>

              <template v-else-if="item.type === 'cascade'">
                <el-cascader
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  v-model="formData[`${item.field}`]"
                  :options="item.options"
                  :props="{
                    label: item.optionsKey,
                    value: item.optionsValue,
                    children: item.optionsChildren,
                    checkStrictly: true
                  }"
                  clearable
                >
                </el-cascader>
              </template>

              <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  v-model="formData[`${item.field}`]"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option[item.optionsValue || 'value']"
                    :label="option[item.optionsKey || 'label']"
                    :value="option[item.optionsValue || 'value']"
                  >
                  </el-option>
                </el-select>
              </template>

              <template v-else-if="item.type === 'datePicker'">
                <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  v-model="formData[`${item.field}`]"
                ></el-date-picker>
              </template>

              <template v-else-if="item.type === 'radio'">
                <el-radio-group
                  v-model="formData[`${item.field}`]"
                  :disabled="item.otherOptions.disabled"
                >
                  <el-radio
                    v-for="option in item.options"
                    :label="option[item.optionsValue || 'value']"
                    :key="option"
                  >
                    {{ option[item.optionsKey || 'label'] }}
                  </el-radio>
                </el-radio-group>
              </template>

              <!-- 表单项插槽 -->
              <template v-else-if="item.type === 'slot'">
                <slot :name="item.slotName" v-bind="item"> </slot>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>

    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { PropType } from 'vue'
import { IFormItem } from '../types'
import { inputChange } from '@/utils/inputChange'

const emit = defineEmits(['update:modelValue'])
const props = defineProps({
  modelValue: {
    type: Object,
    required: true
  },
  formItems: {
    type: Array as PropType<IFormItem[]>,
    default: () => []
  },
  labelWidth: {
    type: String,
    default: '100px '
  },
  itemStyle: {
    type: Object,
    default: () => ({ padding: '10px 40px' })
  },
  colLayout: {
    type: Object,
    default: () => ({
      xl: 6, // >1920px 4个
      lg: 8,
      md: 12,
      sm: 24,
      xs: 24
    })
  }
})
const formData = ref({ ...props.modelValue })
const formRef = ref<FormInstance>()

watch(
  props.modelValue,
  (newValue) => {
    formData.value = newValue
  },
  {
    deep: true
  }
)

watch(
  formData,
  (newValue) => {
    emit('update:modelValue', newValue)
  },
  {
    deep: true
  }
)

defineExpose({ formRef, formData })
</script>

<style scoped lang="less">
.hy-form {
  padding-top: 22px;
}
:deep(.el-input__wrapper) {
  width: 100%;
}
</style>
